package setting

import (
	"github.com/go-ini/ini"
	log "github.com/sirupsen/logrus"
        "strings"
	//"os"
//	"path"
)

var cfg *ini.File
var err error

type GConfig struct {
    LogLevel    int
    Port        string
    Token       string
    StoreType   string
    LogSavePath string
}

type PgDBConfig  struct {
    DbUrl        string
    DbSchema     string
    DbUser       string
    DbPassword   string
    DbName       string
}

type MogDBConfig  struct {
    DbUrl        string
    DbAuthdb     string
    DbUser       string
    DbPassword   string
    DbName       string
    SaveAllField string
}

var GlobalSetting = &GConfig{}
var PgdbSetting = &PgDBConfig{}
var MogdbSetting = &MogDBConfig{}
var StoreType string

func Setup(conf string) {
    /*
    Mpath, err := os.Executable()
    if err != nil {
	log.Errorln(err)
    }
    */ 
    
    //cfg, err = ini.Load(path.Dir(Mpath) + conf)
    cfg, err = ini.Load(conf)
    if err != nil {
	log.Errorln("Fail to parse 'app.ini': %v", err)
	return
    }

    mapTo("Global", GlobalSetting)
    switch strings.TrimSpace(GlobalSetting.StoreType) {
    case "MongoDB":
        mapTo("MongoDB",MogdbSetting)
    case "PostgreSQL":
        mapTo("PostgreSQL",PgdbSetting)
    default:
    }

/*
    log.Fatalf(GlobalSetting.StoreType)
*/   
}

func mapTo(section string, v interface{}) {
    err := cfg.Section(section).MapTo(v)
    if err != nil {
	log.Errorln("Cfg.MapTo RedisSetting err: %v", err)
	return
    }
}
